<template>
  <div>
    <!-- 课程列表 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="APP名称">
        <el-select
          v-model="formInline.app_id"
          @change="onSubmit"
          placeholder="请选择APP名"
        >
          <el-option
            v-for="item in selectArr.appid"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          @change="onSubmit"
          v-model="formInline.date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy 年 MM 月 dd 日"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="名称">
        <el-input
          v-model="formInline.name"
          placeholder="请输入banner名称"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addBanner()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      border
      :show-header="fuck"
      style="width: 100%"
      header-cell-class-name="thead"
    >
      <el-table-column prop="date" width="368">
        <template slot-scope="scope">
          <el-image
            fit="cover"
            class="u-banner"
            :src="scope.row.banner_url"
            :preview-src-list="[scope.row.banner_url]"
          ></el-image>
          <!-- <img  :src="scope.row.banner_url" alt /> -->
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="300">
        <template slot-scope="scope">
          <abbr title="探索太空之旅">
            <h2>{{ scope.row.name }}</h2>
          </abbr>
          <abbr :title="scope.row.description">
            <p>{{ scope.row.description }}</p>
          </abbr>
        </template>
      </el-table-column>
      <!-- <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <p>1798PV</p>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <p>830UV</p>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <p>120转发</p>
        </template>
      </el-table-column>-->
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <p>{{ scope.row.create_time }} 发布</p>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <div>
            <a @click="addBanner(scope.row.id)">编辑</a>
            <a @click="copyUrl(scope.row.content_url)">复制链接</a>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>
<script>
import { bannerList } from '@/api/app.js'
import { queyEnumByTableName } from '@/api/api.js'
export default {
  name: 'asdasdaaqweqs',
  data () {
    return {
      selectArr: {
        status: [],
        appid: []
      },
      fuck: false,
      pageIndex: 1,
      value: 2,
      value2: 3,
      pageSize: 10,
      totalPage: 0,
      formInline: {
        app_id: '',
        date: [],
        name: ''
      },
      tableData: []
    }
  },
  activated () {
    this.getSelectData()
  },
  methods: {
    // 下拉菜单过滤
    filterArr (field, val) {
      let arr = this.selectArr[field]
      let label
      arr.map(item => {
        if (parseInt(val) === parseInt(item.real_value)) {
          label = item.label
        }
        if (item.children && item.children.length > 0) {
          item.children.map(item2 => {
            if (parseInt(val) === parseInt(item2.real_value)) {
              label = item.label
            }
          })
        }
      })
      return label
    },
    // 获取下拉菜单数据
    async getSelectData () {
      let res = await queyEnumByTableName({ tableName: 'nice_course_banner' })
      if (res[0]) {
        return
      }
      let arr = res[1].data
      Object.keys(this.selectArr).forEach(item => {
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].fieldName === item) {
            this.selectArr[item] = arr[i].child
          }
        }
      })
      this.selectArr = JSON.parse(
        JSON.stringify(this.selectArr)
          .split('display_value')
          .join('label')
          .split('child')
          .join('children')
          .split('t_id')
          .join('value')
      )
      this.formInline.app_id = this.selectArr.appid[0].value
      this.getTableData()
    },
    // 获取表数据
    async getTableData () {
      let res = await bannerList({
        'nice_course_banner.app_id': this.formInline.app_id,
        'nice_course_banner.start_date': this.formInline.date[0],
        'nice_course_banner.end_date': this.formInline.date[1],
        'nice_course_banner.name': this.formInline.name,
        'page.currPage': this.pageIndex,
        'page.pageSize': this.pageSize
      })
      if (res[0]) {
        return
      }
      this.totalPage = res[1].data.pageList.totalCount
      this.tableData = res[1].data.pageList.list

      console.log(res[1])
    },
    // 老师名字过滤器
    teacherNameArr (e, type) {
      if (type === 'all') {
        return e.join('、')
      } else {
        if (e && e.length < 2) {
          if (e[0].length > 6) {
            return e[0].substr(0, 6) + '...'
          } else {
            return e[0]
          }
        } else {
          let name = e[0] + '、' + e[1]
          if (name.length > 6) {
            return name.substr(0, 6) + '...'
          } else {
            return name
          }
        }
      }
    },
    // 字数过滤器
    fontLength (e) {
      if (e && e.length > 20) {
        return e.substr(0, 20) + '...'
      } else {
        return e
      }
    },
    copyUrl (e) {
      var Url2 = e
      var oInput = document.createElement('input')
      oInput.value = Url2
      document.body.appendChild(oInput)
      oInput.select() // 选择对象
      document.execCommand('Copy') // 执行浏览器复制命令
      oInput.className = 'oInput'
      oInput.style.display = 'none'

      // window.clipboardData.setData('Text', e)
      this.$message({
        message: '复制成功!',
        type: 'success'
      })
    },
    addBanner (e) {
      e
        ? this.$router.push({
          name: 'add-banner',
          query: { id: e, appId: this.formInline.app_id }
        })
        : this.$router.push({
          name: 'add-banner',
          query: { id: e, appId: this.formInline.app_id }
        })
    },
    addUpdateCourse (e) {
      if (e) {
        this.$router.push({ name: 'add-update-class', query: { id: 1 } })
      } else {
        this.$router.push({ name: 'add-update-class' })
      }
    },
    onSubmit () {
      this.pageIndex = 1
      this.getTableData()
      console.log('submit!')
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getTableData()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getTableData()
    }
  }
}
</script>
<style scoped>
abbr {
  text-decoration: none;
}
.z-display-flex {
  display: flex;
}
a {
  cursor: pointer;
}
.u-banner {
  width: 368px;
  height: 173px;
  object-fit: cover;
}
</style>
